<template>
  <div class="app-container">
    <div v-show="!purchOrderCheckApplicationInfo.visible">
      <!-- 条件栏 -->
      <PageFilter
        v-model:query="filterInfo.query"
        :filter-list="filterInfo.list"
      />
      <NewPageFilter :filter-btn="filterInfo.btnList"></NewPageFilter>
      <!-- 表格 -->
      <TableContainer
        :refresh="tableInfo.refresh"
        :init-curpage="tableInfo.initCurpage"
        v-model:data="tableInfo.data"
        :api="tableInfo.api"
        :tab-index="true"
        :check-box="true"
        v-model:query="filterInfo.query"
        :field-list="tableInfo.fieldList"
        :handle="tableInfo.handle"
        @handleEvent="handleEvent"
      >
        <!-- <template v-slot:col-businessLicense="scope">
          <p>{{ scope.row.businessLicense }}</p>
          <p>{{ scope.row.businessLicenseWarningTypeValue }}</p>
        </template> -->
      </TableContainer>
    </div>
    <page-dialog
      v-if="pageDialogInfo.visible"
      :title="pageDialogInfo.title"
      v-model:visible="pageDialogInfo.visible"
      :width="pageDialogInfo.width"
      :bt-loading="pageDialogInfo.btLoading"
      :bt-list="pageDialogInfo.btList"
      @reset="closeDialog"
    >
      <page-form
        class="paddingBox order-model-formBox flex_wrap_start_start"
        v-model:ref-obj="pageFormInfo.ref"
        :data="pageFormInfo.data"
        :field-list="pageFormInfo.fieldList"
        :label-width="pageFormInfo.labelWidth"
      />
    </page-dialog>
    <checkPurchApplication
      v-if="purchOrderCheckApplicationInfo.visible"
      v-model:visible="purchOrderCheckApplicationInfo.visible"
      :purchApplicationId="purchOrderCheckApplicationInfo.orderId"
      :purch-application-info="purchOrderCheckApplicationInfo.info"
      :purch-type="purchOrderCheckApplicationInfo.purchType"
      :purch-according="purchOrderCheckApplicationInfo.purchAccording"
      :selectInfo="tableInfo.selections"
      @returnModify="returnModify"
    />
  </div>
</template>

<script setup name="bandQuantityProductWarning">
import {
  bandQuantityExpireWarning,
  bandQuantityExpireWarningAll,
} from '@/api/warning'
import checkPurchApplication from './checkPurchApplication'
import { getReceiveAddressAll } from '@/api/core'
const { proxy } = getCurrentInstance()

// 过滤相关配置
const filterInfo = ref({
  query: {
    businessLicenseWarningType: '',
    medicalDeviceLicenseWarningType: '',
    firstMedicalDeviceFilingWarningType: '',
    sanitaryLicenseWarningType: '',
  },
  list: [
    {
      label: '产品名称',
      value: 'nameLike',
      type: 'input',
      event: search,
    },
    {
      label: '产品编码',
      value: 'codeLike',
      type: 'input',
      event: search,
    },
    { type: 'button', label: '搜索', event: search },
  ],
  btnList: [
    {
      type: 'button',
      label: '生成采购单',
      show: proxy.$permission('bandQuantityProductWarning:purchaseOrder'),
      event: generatePurchOrder,
    },
  ],
})
// 表格相关
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  api: bandQuantityExpireWarning,
  data: [],
  selections: [],
  fieldList: [
    { label: '产品名称', value: 'commonName' },
    { label: '产品编码', value: 'productCode' },
    { label: '产品采购下限', value: 'lowLimit' },
    { label: '已采购数量', value: 'purchQty' },
    // { label: "预采购数量", value: "code" },
    { label: '差额数量', value: 'balanceQty' },
  ],
  // handle: {
  //   fixed: "right",
  //   label: "操作",
  //   width: "140",
  //   btList: [
  // {
  //   type: "button",
  //   label: "生成采购单",
  //   show: proxy.$permission("supplier:add"),
  //   event: generatePurchOrder,
  // }
  //   ],
  // },
})
const selectRow = ref({})
const purchOrderCheckApplicationInfo = reactive({
  visible: false,
  title: '通过申购单生成采购单',
  orderId: null,
  info: undefined,
  purchType: null,
})
const pageDialogInfo = ref({
  title: '选择采购依据',
  visible: false,
  type: '',
  width: '400px',
  row: {},
  btLoading: false,
  btList: [
    {
      label: '关闭',
      type: '',
      icon: '',
      event: closeDialog,
      show: true,
    },
    {
      label: '保存',
      type: 'primary',
      icon: '',
      event: saveDialog,
      saveLoading: false,
      show: true,
    },
  ],
})
const pageFormInfo = ref({
  ref: null,
  data: {},
  fieldList: [
    {
      label: '采购依据',
      value: 'purch_according',
      type: 'select-model',
      code: 'purch_according',
    },
  ],
})

getList()
// 获取列表
function getList() {
  tableInfo.value.refresh = Math.random()
}
function returnModify() {
  if (purchOrderCheckApplicationInfo.purchApplicationId) {
    getList()
  } else {
    search()
  }
}
function closeDialog() {
  pageDialogInfo.value.visible = false
  pageFormInfo.value.data = {}
}
function generatePurchOrder(row) {
  if (!tableInfo.value.selections.length) {
    proxy.$message({
      message: `请选择产品`,
      type: 'warning',
    })
    return
  }
  pageDialogInfo.value.visible = true
}
function saveDialog() {
  purchOrderCheckApplicationInfo.orderId = selectRow.value.id
  purchOrderCheckApplicationInfo.info = selectRow.value
  purchOrderCheckApplicationInfo.purchType = selectRow.value.purchType
  purchOrderCheckApplicationInfo.purchAccording =
    pageFormInfo.value.data.purch_according
  closeDialog()
  proxy.$nextTick(() => {
    purchOrderCheckApplicationInfo.visible = true
  })
}
// 搜索
function search() {
  // 重置分页
  tableInfo.value.initCurpage = Math.random()
  tableInfo.value.refresh = Math.random()
}

// 触发事件
function handleEvent(event, data) {
  switch (event) {
    case 'tableCheck':
      tableInfo.value.selections = [...data]
      break
  }
}
</script>

<style scoped lang="scss">
:deep(.el-table) .nearly-row {
  background: #fceacb;
}
:deep(.el-table) .expire-row {
  background: #f5e4e4;
}
:deep(el-table__body) tr.hover-row > td {
  background-color: transparent !important;
}
</style>
